package com.hrt.manage.product.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftType;
import com.hrt.bean.GiftTypeDir;
import com.hrt.manage.product.dao.GiftTypeDirDao;
import com.hrt.manage.product.service.GiftInforService;
import com.hrt.manage.product.service.GiftTypeDirService;
import com.hrt.manage.product.service.GiftTypeService;
import com.hrt.vo.GiftTypeDirVO;

@Transactional
@Service("giftTypeDirService")
public class GiftTypeDirServiceImpl implements GiftTypeDirService {

	/**礼品分类目录明细*/
	@Autowired
	private GiftTypeDirDao giftTypeDirdao; 
	
	/**礼品分类*/
	@Autowired
	private GiftTypeService giftTypeService;
	
	/**礼品基本信息*/
	@Autowired
	private GiftInforService giftInforService;
	
	/**
	 * 保存实体
	 * */
	@Override
	public void addGiftTypeDir(GiftTypeDir GiftTypeDir) {
		this.giftTypeDirdao.saveEntity(GiftTypeDir);
		
	}
	
	/**
	 * 更新实体
	 * */
	@Override
	public void updateGiftTypeDir(GiftTypeDir GiftTypeDir) {
		this.giftTypeDirdao.updateEntity(GiftTypeDir);
		
	}
	
	/**
	 * 删除实体
	 * */
	@Override
	public void delGiftTypeDir(Long id) {
		this.giftTypeDirdao.deleteEntity(id);
		
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public GiftTypeDir getById(Long id) {
		
		return this.giftTypeDirdao.getById(id);
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<GiftTypeDir> searchAll() {
		
		return this.giftTypeDirdao.findAll();
	}

	/**
	 * 按是否删除查询
	 */
	@Override
	public List<GiftTypeDir> searchDelAll() {
		
		return this.giftTypeDirdao.findDelAll();
	}

	/**
	 * 按id组列表查询实体
	 * */
	@Override
	public List<GiftTypeDir> getByIds(Long[] ids) {
		
		return this.giftTypeDirdao.getByIds(ids);
	}

	/**分页*/
	@Override
	public Long totalCount() {
		
		return this.giftTypeDirdao.totalCount();
	}

	@Override
	public List<GiftTypeDir> queryByPage(int pageIndex, int pageSize) {
		
		return this.giftTypeDirdao.queryByPage(pageIndex, pageSize);
	}
	
	/**获取数据库的当前Timestamp形式的时间*/
	@Override
	public Timestamp getTimestamp() {
		
		return this.giftTypeDirdao.getDate();
	}
	
	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<GiftTypeDir> delResult = giftTypeDirdao.getByIds(ids);
		for (GiftTypeDir  gt: delResult) {
			gt.setIsdelete(new Integer(1));
			this.giftTypeDirdao.updateEntity(gt);
		}
	}
	
	
	/**删除一个id的数据*/
	@Override
	public void deleteByOneId(Long id) throws Exception {
		GiftTypeDir gt = giftTypeDirdao.getById(id);
		gt.setIsdelete(new Integer(1));
		this.giftTypeDirdao.updateEntity(gt);
	}
	
	
	/**
	 * 根据id删除订单详情
	 * 
	 */
	@Override
	public boolean delItemById(Long itemId) {
		GiftTypeDir item = this.giftTypeDirdao.getById(itemId);
		if (item != null) {
			item.setIsdelete(new Integer(1));
			this.giftTypeDirdao.updateEntity(item);
			return true;
		}
		return false;
	}
	
	
	/**
	 *将GiftTypeVO转换为GiftType
	 */
	@Override
	public void updateGiftTypeDirs(List<GiftTypeDirVO> gTypeDirs) {
		try {
			for(GiftTypeDirVO gTypeDir: gTypeDirs) {
				GiftTypeDir giftTypeDir = new GiftTypeDir();
				if(gTypeDir.getId()==null){
					BeanUtils.copyProperties(giftTypeDir, gTypeDir);
					GiftType giftType = this.giftTypeService.getById(gTypeDir.getTypeId());
					//礼品分类
					giftTypeDir.setGiftType(giftType);
					
					//礼品分类id
					giftTypeDir.setTypeId(gTypeDir.getTypeId());
					
					//礼品分类名称
					giftTypeDir.setTypeName(giftType.getTypeName());
					
					//礼品基本信息的实体
					giftTypeDir.setGiftInfor(this.giftInforService.getById(gTypeDir.getGiftId()));
					/**添加*/
					this.addGiftTypeDir(giftTypeDir);
				}else{
					BeanUtils.copyProperties(giftTypeDir, gTypeDir);
					GiftType giftType = this.giftTypeService.getById(gTypeDir.getTypeId());
					//礼品分类
					giftTypeDir.setGiftType(giftType);
					
					//礼品分类id
					giftTypeDir.setTypeId(gTypeDir.getTypeId());
					
					//礼品分类名称
					giftTypeDir.setTypeName(giftType.getTypeName());
					
					//礼品基本信息的实体
					giftTypeDir.setGiftInfor(this.giftInforService.getById(gTypeDir.getGiftId()));
					/**修改*/
					this.updateGiftTypeDir(giftTypeDir);
				}
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	/**根据礼品分类的ID找到礼品分类目录明细的列表*/
	@Override
	public List<GiftTypeDir> getGiftTypeDirByGdId(Long typeId) {
		return this.giftTypeDirdao.queryGiftTypeDirByGdId(typeId);
	}
	/**根据礼品基本信息的ID找到礼品分类目录明细的列表*/
	@Override
	public List<GiftTypeDir> getGiftTypeDirByGIId(Long giftId) {
		return this.giftTypeDirdao.getGiftTypeDirByGIId(giftId);
	}

}
